package com.zufangbao.earth.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.zufangbao.earth.entity.particalsystem.AppParticles;
import com.zufangbao.earth.service.AppParticlesService;
import com.zufangbao.earth.service.ParticalService;
import com.zufangbao.gluon.exception.CommonException;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth;
import com.zufangbao.gluon.spec.global.GlobalMsgSpec.GeneralErrorMsg;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.service.AppService;

@Service("appParticlesService")
public class AppParticlesServiceImpl extends GenericServiceImpl<AppParticles> implements AppParticlesService {
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Autowired
	private ParticalService particalService;
	
	@Autowired
	private AppService appService;

	@Override
	public void saveAppParticle(AppParticles appParticle) throws CommonException {
		
		if(null == appParticle){
			
			throw new CommonException(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		String uniqueParticalId = appParticle.getUniqueParticalId();
		
		if(StringUtils.isEmpty(uniqueParticalId)){
			
			throw new CommonException(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		if(null == particalService.getParticalByUniqueId(appParticle.getUniqueParticalId())){
			
			throw new CommonException(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		if(StringUtils.isEmpty(appParticle.getAppId())){
			
			throw new CommonException(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
		}
		
		App app = appService.getApp(appParticle.getAppId());
		
		if(null == app){
			
			throw new CommonException(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS, GeneralErrorMsg.MSG_ILLEGAL_PARAM);
			
		}
		
		genericDaoSupport.save(appParticle);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppParticles> getAppParticleByAppId(String appId) {
		
		List<AppParticles> appParticleList = new ArrayList<AppParticles>();
		
		if(StringUtils.isEmpty(appId)){
			
			return appParticleList;
		}
		
		String querySentence  = "FROM AppParticles appParticles WHERE appParticles.appId = :appId AND appParticles.isDelete = false ";
		
		appParticleList = genericDaoSupport.searchForList(querySentence, "appId",appId);
		
		return appParticleList;
	}

	@Override
	public void deleteAppParticle(Long appParticleId) {

		if(StringUtils.isEmpty(appParticleId)){
			
			return;
		}
		AppParticles appParticles = genericDaoSupport.get(AppParticles.class, appParticleId);
		
		appParticles.setDelete(Boolean.TRUE);
		
		genericDaoSupport.saveOrUpdate(appParticles);
	}

	@Override
	public void updateAppParticleWithUniqueId(String originalUniqueId,
			String newUniqueId) {
		
		if(StringUtils.isEmpty(originalUniqueId) || StringUtils.isEmpty(newUniqueId)){
			
			return;
		}
		@SuppressWarnings("unchecked")
		List<AppParticles> appParticleList = genericDaoSupport.searchForList("FROM AppParticles WHERE uniqueParticalId = :uniqueParticalId", "uniqueParticalId", originalUniqueId);
			
		for(AppParticles item : appParticleList){
			
			item.setUniqueParticalId(newUniqueId);
			
			genericDaoSupport.saveOrUpdate(item);
		}
	}

	@Override
	public List<String> getQuarkParticalUniqueIds(String appId) {
		
		List<String> particalUniqueIds = new ArrayList<String>();
		
		if(StringUtils.isEmpty(appId)){
			
			return particalUniqueIds;
		}
		
		List<AppParticles> appParticles = getAppParticleByAppId(appId);
		
		for(AppParticles item : appParticles){
			
			if(particalService.isQuarkPartical(item.getUniqueParticalId())){
				
				particalUniqueIds.add(item.getUniqueParticalId());
			}
		}
		return particalUniqueIds;
	}

	@Override
	public List<AppParticles> getQuarkPartical(String appId) {
		
		List<AppParticles> quarkAppParticles = new ArrayList<AppParticles>();
		
		if(StringUtils.isEmpty(appId)){
			
			return Collections.EMPTY_LIST;
		}
		
		List<AppParticles> appParticles = getAppParticleByAppId(appId);
		
		for(AppParticles item : appParticles){
			
			if(particalService.isQuarkPartical(item.getUniqueParticalId())){
				
				quarkAppParticles.add(item);
			}
		}
		return quarkAppParticles;
	}

}
